package com.myweb.controller.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;



/**
 * 文件上传控制器
 * 
 * @author SunTo
 * 
 */
@Controller
@RequestMapping("upload")
public class UploadController {
	
	private static final Logger logger = Logger.getLogger(UploadController.class);
	
	@RequestMapping(value = "test")
	public String test(HttpServletRequest request){
		// 获取文件 存储位置
		String realPath = request.getSession().getServletContext().getRealPath("/");
		realPath = realPath.substring(0,realPath.length() - 14)+"SM_UploadFile";

		return realPath;
	}

	@ResponseBody
	@RequestMapping("file")
	public String uploadimg(MultipartFile file, HttpServletRequest request) {
		
		try {
			//获得本机IP 
			//String addr = InetAddress.getLocalHost().getHostAddress();
			String addr = "localhost:8080/SM_UploadFile";

			String uuid = UUID.randomUUID().toString().replaceAll("-", "");
			// 获取文件 存储位置(webapp下SM_UploadFile)
			String realPath = request.getSession().getServletContext().getRealPath("");
			realPath = realPath.substring(0,realPath.length() - 14)+"SM_UploadFile/";
			
			//**备份的文件夹地址**
			String backupPath = realPath.substring(0,realPath.length() - 14)+"SM_Backup/";
			
			String imgType = file.getOriginalFilename();
			String filename = uuid + imgType.substring(imgType.lastIndexOf('.'));
			File file_url=	new File(realPath + "/");
			
			
			//如果文件夹不存在则创建   
			if(!file_url .exists()  && !file_url .isDirectory()){
				file_url .mkdir();  
			}
			File fiel_url_fiel=new File(realPath + "/"+filename);
			//如果文件不存在则创建   
			if(!fiel_url_fiel.exists()){
				fiel_url_fiel.createNewFile();
			}
			// 将文件copy上传到服务器
			file.transferTo(fiel_url_fiel);
			
			//**备份的文件夹**
			File backup_url=	new File(backupPath + "/");
			if(!backup_url .exists()  && !backup_url .isDirectory()){
				backup_url .mkdir();  
			}
			File backup_url_fiel=new File(backupPath + "/"+filename);
			if(!backup_url_fiel.exists()){
				backup_url_fiel.createNewFile();
			}
			//**备份**
			copyFile(fiel_url_fiel,backup_url_fiel);
			
			return addr+="/" + filename;
		} catch (Exception e) {
			return "失败";
		}
		
	}
	
	  public static void copyFile(File fromFile,File toFile) throws IOException{
	        FileInputStream ins = new FileInputStream(fromFile);
	        FileOutputStream out = new FileOutputStream(toFile);
	        byte[] b = new byte[1024];
	        int n=0;
	        while((n=ins.read(b))!=-1){
	            out.write(b, 0, b.length);
	        }
	        
	        ins.close();
	        out.close();
	    }
}
